﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lucid.ESA.ClientDataSystem.Application.Contracts;
using Lucid.ESA.ClientDataSystem.Application.Responses;
using Lucid.ESA.ClientDataSystem.Application.DataTransferObjects;
using StructureMap;
using Lucid.ESA.ClientDataSystem.Application.Requests;

namespace Lucid.ESA.ClientDataSystem.UI.Controllers
{
    public class SiteController : Controller
    {
        public ActionResult Index(long clientId)
        {
            IClientService clientService = ObjectFactory.GetInstance<IClientService>();
            GetResponse<ClientDto> response = clientService.GetClientById(clientId) as GetResponse<ClientDto>;
            ViewData.Add("clientId", clientId);
            return View(response.Value.Sites);
        }

        public ActionResult Details(long id)
        {
            ISiteService siteService = ObjectFactory.GetInstance<ISiteService>();
            GetResponse<SiteDto> response = siteService.GetSiteById(id) as GetResponse<SiteDto>;
            return View(response.Value);
        }

        public ActionResult Edit(long id)
        {
            ISiteService siteService = ObjectFactory.GetInstance<ISiteService>();
            SiteDto siteDto = (siteService.GetSiteById(id) as GetResponse<SiteDto>).Value;

            EditSiteRequest editSiteRequest = new EditSiteRequest
            {
                SiteId = siteDto.Id.Value,
                AddressLine1 = siteDto.AddressLine1,
                AddressLine2 = siteDto.AddressLine2,
                AddressLine3 = siteDto.AddressLine3,
                AddressLine4 = siteDto.AddressLine4,
                MainSite = siteDto.MainSite,
                PostCode = siteDto.PostCode
            };

            ViewData.Add("clientId", siteDto.Client.Id.Value);
            return View(editSiteRequest);
        }

        [HttpPost]
        public ActionResult Edit(EditSiteRequest request, long clientId)
        {
            ISiteService siteService = ObjectFactory.GetInstance<ISiteService>();
            Response response = siteService.EditSite(request);

            if (response.Status == ResponseStatus.Success)
            {
                return RedirectToAction("/Index", new { clientId = clientId });
            }
            else
            {
                ValidationErrorResponse validationErrorResponse = response as ValidationErrorResponse;
                foreach (ValidationErrorResponseMessage error in validationErrorResponse.Errors)
                    ViewData.ModelState.AddModelError(error.Field == null ? "" : error.Field, error.Text);

                return Edit(request.SiteId);
            }
        }

        public ActionResult Create(long clientId)
        {
            ViewData.Add("clientId", clientId);
            return View();
        }

        [HttpPost]
        public ActionResult Create(CreateSiteRequest request)
        {
            ISiteService siteService = ObjectFactory.GetInstance<ISiteService>();
            Response response = siteService.CreateSite(request);

            if (response.Status == ResponseStatus.Success)
            {
                return RedirectToAction("/Index", new { clientId = request.ClientId });
            }
            else
            {
                ValidationErrorResponse validationErrorResponse = response as ValidationErrorResponse;
                foreach (ValidationErrorResponseMessage error in validationErrorResponse.Errors)
                    ViewData.ModelState.AddModelError(error.Field == null ? "" : error.Field, error.Text);

                return Create(request.ClientId);
            }
        }
    }
}
